<?php


namespace App\servers;


class RlySmsServer implements SendsmsInterface
{
    /**
     * 容联云发送验证码
     * @param $phone
     * @param bool $debug
     * @return bool
     */
    public function send($phone,$debug = false)
    {
        include_once public_path().'/sms/SDK/CCPRestSDK.php';
        //主帐号
        $accountSid = config('code.rongly.accountSid');
        //主帐号Token
        $accountToken = config('code.rongly.accountToken');
        //应用Id
        $appId = config('code.rongly.appid');
        //请求地址，格式如下，不需要写https://,默认为：app.cloopen.com'
        $serverIP = config('code.rongly.serverIP');
        //请求端口 默认为：8883
        $serverPort = config('code.rongly.serverPort');
        //REST版本号 默认 为：2013-12-26
        $softVersion =config('code.rongly.softVersion');
        //短音验证码
        $code = rand(1111,9999);
        $datas =[$code];
        //短信模板编号
        $tempId = 1;
//        if($debug){
//            $resdis = RedisServices::getInstance()->getRedis();
//            if($resdis->get($phone)) abort(40001,'请不要频繁发送');
//            $resdis->set($phone,$code,60);
//            return true;
//        }
        // 初始化REST SDK
        $rest = new \REST($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);


        // 发送模板短信
        $result = $rest->sendTemplateSMS($phone, $datas, $tempId);
        if ($result == NULL) {
            abort(401, "result error!");
        }
        if ($result->statusCode != 0) {
            abort(401,$result->statusCode.':'.$result->statusMsg );
            //TODO 添加错误处理逻辑
        } else {
            //调用redis单例
//            $resdis = RedisServer::entrance();
//            if($resdis->get($phone)) abort(40001,'请不要频繁发送');
//            $resdis->set($phone,$code,60);
            return true;
            //TODO 添加成功处理逻辑
        }
    }
}
